/*
 * A RMI spelling client with full functions of add, check and remove.
 * Written by YUE CHI
 */

import java.net.SocketException;
import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.SortedSet;

/**
 * A RMI spelling client with full functions of add, check and remove.
 * For test.
 * @author YUE CHI
 * @version 1.0
 */
public class RMISpellingClientFullfunction extends AbstractClient
{
  /**
   * The check word.
   */
  public static final String CHECKWORD = "check";
  
  /**
   * The check word.
   */
  public static final String ADDWORD = "add";

  /**
   * The check word.
   */
  public static final String REMOVEWORD = "remove";


  /**
   * The constructor.
   * @param the_args  Input string from commandline.
   *                 The input should be <host><port><service name><check/add/remove><word..>
   * @throws SocketException  if here is socket problems
   */
  public RMISpellingClientFullfunction(final String[] the_args) throws SocketException
  {
    super(the_args);
  }

  /**
   * Main function.
   * @param the_args  input string from commandline.
   * @throws SocketException  socket problem.
   */
  public static void main(final String[] the_args) throws SocketException
  {
    final RMISpellingClientFullfunction client = new RMISpellingClientFullfunction(the_args);
    try
    {
      final RemoteSpelling temp = (RemoteSpelling) (client.my_registery.lookup(the_args[2]));
      final Iterator iterator_word = client.my_searchlist.iterator();
      final String function = iterator_word.next().toString();
      if (function.equals("check"))
      {
        while (iterator_word.hasNext())
        {
          final String wordcheck = (String) iterator_word.next();
          final SortedSet<String> tempsuggestions = temp.check(wordcheck);
          Log.out("Querying service for word " + wordcheck);
          if (tempsuggestions == null)
          {
            Log.out(wordcheck + " is spelled correctly");
          }
          else
          {
            final int num_suggestions = tempsuggestions.size();
            if (num_suggestions > 0)
            {
              final StringBuffer sb = new StringBuffer();
              sb.append(wordcheck + "is spelled incorrctly, there are " + num_suggestions +
                        " suggestions");
              final Iterator iterator_suggestions = tempsuggestions.iterator();
              while (iterator_suggestions.hasNext())
              {
                final String tempsuggestion = (String) iterator_suggestions.next();
                sb.append(' ');
                sb.append(tempsuggestion);
              }
              Log.out(sb.toString());
            }
            else
            {
              Log.out(wordcheck + " is spelled incorrectly, no suggestions");
            }
          }

        }
      }
      else if (function.equals("add"))
      {
        while (iterator_word.hasNext())
        {
          final String wordadd = (String) iterator_word.next();
          temp.add(wordadd);
          Log.out("Successfully add");
        }

      }
      else if (function.equals("remove"))
      {
        while (iterator_word.hasNext())
        {
          final String wordadd = (String) iterator_word.next();
          temp.remove(wordadd);
          Log.out("Successfully remove");
        }

      }
      return;

    }
    catch (final AccessException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (final RemoteException e)
    {
      Log.err("Error looking up or  accessing " + e);
      return;
    }
    catch (final NotBoundException e)
    {
      Log.err("Spelling service cannot bound on registery " + e);
      e.printStackTrace();
    }
  }

}
